<template>
  <el-dialog
    class="add-user-comp"
    :title="title"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(900)"
    :before-close="handleCancel">
    <div class="form-container">
      <el-form
        element-loading-custom-class="custom-load"
        :model="ruleForm"
        :rules="rules"
        size="medium"
        ref="ruleForm"
        :label-width="$setRem(120)"
        class="rule-form"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="userName" required>
              <el-input
                type="text"
                clearable
                placeholder="请输入姓名"
                :maxlength="50"
                show-word-limit
                v-model="ruleForm.userName">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender" required>
              <el-radio-group v-model="ruleForm.gender" size="medium">
                <el-radio-button label="女">女</el-radio-button>
                <el-radio-button label="男">男</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="userId" required>
              <el-input
                type="text"
                placeholder="请输入用户名"
                :maxlength="50"
                clearable
                show-word-limit
                v-model="ruleForm.userId">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="emailAddress">
              <el-input
                type="text"
                clearable
                :maxlength="50"
                show-word-limit
                placeholder="请输入邮箱"
                v-model="ruleForm.emailAddress">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="telephoneNumber" required>
              <el-input
                type="text"
                :maxlength="11"
                show-word-limit
                placeholder="请输入联系方式"
                v-model="ruleForm.telephoneNumber"
                clearable>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col v-if="type === 'add'" :span="12">
<!--            <el-form-item label="密码" prop="password">-->
<!--              <el-input-->
<!--                type="password"-->
<!--                placeholder="请输入密码"-->
<!--                :value="ruleForm.password"-->
<!--                :show-password="true"-->
<!--                autocomplete="new-password">-->
<!--              </el-input>-->
<!--            </el-form-item>-->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleCancel">取 消</el-button>
      <el-button
        size="medium"
        type="primary"
        :loading="loading"
        @click="hanldeSave">
        保 存
      </el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  name: 'addUser',
  props: {
    // 弹框是否展示
    visible: {
      type: Boolean,
      default: false
    },
    option: {
      type: Object,
      default: () => ({})
    },
    loading: {
      type: Boolean,
      default: false
    },
    // 新增、编辑
    type: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      // form表单数据
      ruleForm: {
        uuid: '',
        userName: '',
        userId: '',
        gender: '女',
        telephoneNumber: '',
        password: '123',
        emailAddress: ''
      },
      // form 表单校验规则
      rules: {
        userName: [
          { required: true, message: '请输入用户' },
        ],
        gender: [
          { required: true, message: '请选择性别', },
        ],
        userId: [
          { required: true, message: '请输入用户名', },
        ],
        // password: [
        //   { required: true, message: '请输入密码', },
        // ],
        telephoneNumber: [
          { required: true, message: '请输入手机号' },
          { message: '手机号格式不对', pattern: /^1[3456789]\d{9}$/, trigger: 'blur' },
        ],
        // roleId: [
        //   { required: true, message: '请选择所属角色', trigger: 'change' },
        // ]
      },
    };
  },
  computed: {
    // 设置弹框标题
    title() {
      return `${this.type === 'add' ? '新增' : '编辑'}用户`;
    },
  },
  watch: {
    // 监听弹框显示/隐藏
    visible(value) {
      if (!value) {
        Object.keys(this.ruleForm).forEach(key => {
          this.ruleForm[key] = key === 'gender'
            ? '女'
            : key === 'password' ? '123' : '';
        });
      }
    },
    // 回填数据
    option: {
      deep: true,
      handler: function (data) {
        const keys = Object.keys(this.ruleForm);
        data && Object.keys(data).length && keys.forEach(key => {
          if (key === 'gender') {
            this.ruleForm[key] = data[key] || '女';
            return;
          }
          this.ruleForm[key] = data[key];
        });
      }
    }
  },
  methods: {
    // 保存
    hanldeSave() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('submit', this.ruleForm);
        }
      });
    },
    // 取消
    handleCancel() {
      this.$emit('cancel');
    }
  }
};
</script>

<style lang="scss" scoped>
  .add-user-comp /deep/.el-form-item__content {
    width: 280px;
    .el-select {
      width: 100%;
    }
  }
  .add-user-comp {
    .form-container {
      width: 100%;
      min-height: 300px;
      display: flex;
      align-items: center;
      .el-form {
        width: 100%;
        .el-row {
          margin-bottom: 10px;
        }
      }
    }
  }
</style>
